package com.qhsolution.object;

import java.util.ArrayList;

import com.qhsolution.adapter.WordArrayAdapter;
import com.qhsolution.bdict.DetailActivity;
import com.qhsolution.bdict.MainActivity;
import com.qhsolution.bdict.R;
import com.qhsolution.dbhelper.DataHelper;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Bookmark extends Fragment {
	
	DataHelper dbHelper;

	WordArrayAdapter adapter = null;
	ArrayList<Word> arrWord = new ArrayList<Word>();
	
	ListView lv;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView=inflater.inflate(R.layout.activity_bookmark, container,false);
		final ViewGroup container2 = container;
		dbHelper = new DataHelper(container.getContext());
		lv=(ListView) rootView.findViewById(R.id.listFeaturedWord);
		LoadData();
		adapter=new WordArrayAdapter(getActivity(), R.layout.gridview_word, arrWord);
		adapter.notifyDataSetChanged();
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				//Log.d("PositionView", String.valueOf(position));
				Intent intent = new Intent(container2.getContext(), DetailActivity.class);
				TextView txtId = (TextView) view.findViewById(R.id.txtId);
				String Id = txtId.getText().toString();
				intent.putExtra(MainActivity.WORD_ID, Id);
				startActivity(intent);
			}
			
		});
		
		return rootView;
	}
	
	public void LoadData(){
		dbHelper.openDataBase();
		Cursor c = dbHelper.getSelect("word", null, "featured = ?", new String[]{"1"}, null, null, "eng ASC");
		if(c.getCount()>0 && arrWord.size() <= c.getCount())
		{
			c.moveToFirst();
			while(!c.isAfterLast())
			{
				String name = "";
				if(c.getString(1)!=null)
				{
					name = c.getString(1);
					name = name.replace('"', ' ');
					name = name.trim();
				}
				String eng = "";
				if(c.getString(7)!=null)
				{
					eng = c.getString(7);
					eng = eng.replace('"', ' ');
					eng = eng.trim();
				}
				
				String description = "";
				if(c.getString(2)!=null)
				{
					description = c.getString(2);
					description = description.replace('"', ' ');
					description = description.trim();
				}
				
				Word word = new Word(Integer.parseInt(c.getString(0)),name, description, c.getString(3), eng, Integer.parseInt(c.getString(4)), Integer.parseInt(c.getString(5)), Integer.parseInt(c.getString(6)));
				arrWord.add(word);
				c.moveToNext();
			}
		}
		dbHelper.close();
	}
	
	@Override
	public void onResume()
	{
		if(!arrWord.isEmpty())
		{
			arrWord.clear();
			LoadData();
			adapter.notifyDataSetChanged();
		}
		super.onResume();
	}
	
	@Override
    public void setMenuVisibility(final boolean visible) {
        super.setMenuVisibility(visible);
        if (visible) {
        	//Reload the Data
        	if(!arrWord.isEmpty())
    		{
    			arrWord.clear();
    			LoadData();
    			adapter.notifyDataSetChanged();
    		}
        	else
        	{
        		LoadData();
        		adapter.notifyDataSetChanged();
        	}
            Log.d("BookmarkTab","true");
        }
    }
}